package com.songlanyun.common.exception;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.songlanyun.common.drive.IntegerEnum;

import java.util.Objects;

/**
 * 维护错误码之后也要维护错误描述
 * 1.错误码定义规则为5个数字
 * 2.前面两个代表业务场景，后三位表示错误码，10001：10通用系，001系统未知异常
 * 10：通用
 *      001：参数格式校验
 */
public enum BizCodeEnum implements IntegerEnum<Integer>
{
    UNKNOWN_EXCEPTION(10000,"系统未知异常"),
    VALID_EXCEPTION(10001,"参数格式校验异常");

    private Integer code;
    private String msg;

    BizCodeEnum(Integer code, String msg)
    {
        this.code = code;
        this.msg = msg;
    }


    public void setCode(Integer code)
    {
        this.code = code;
    }

    @Override
    public String getMsg()
    {
        return msg;
    }
    public void setMsg(String msg)
    {
        this.msg = msg;
    }
    @Override
    public Integer getCode()
    {
        return code;
    }

    @JsonCreator
    public static BizCodeEnum getByCode(Integer code) {
        for (BizCodeEnum value : BizCodeEnum.values()) {
            if (Objects.equals(code, value.getCode())) {
                return value;
            }
        }
        return null;
    }
}
